//
//  PAndHViewController.m
//  mafengwo
//
//  Created by Mac on 16/4/17.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "PAndHViewController.h"
#import "SegmentView.h"

#define kButtonW 80
#define kButtonH 40
@interface PAndHViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>{

    UIView *_headView;//头部视图
    UIView *_searchView;//搜索框
    UITableView *_tableView;

}

@end

@implementation PAndHViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //头部视图
    [self createHeadView];
    //搜索框
    [self createSearchButton];

    //tableview
    [self createTableView];
}

-(void)setName:(NSString *)name{

    _name = name;
}

-(void)createHeadView{
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, KScreenW, 44)];
    _headView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_headView];

    //返回摁钮
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10, 4, 30, 30)];
    [button setImage:[UIImage imageNamed:@"back_navig.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:button];

    //中间的title
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(KScreenW/2-40, 4, 80, 40)];
//    title.text = @"机➕酒";
    title.text = _name;
    title.textAlignment = NSTextAlignmentCenter;
    [_headView addSubview:title];

}
//模态视图 返回
-(void)backAction{

    [self dismissViewControllerAnimated:NO completion:nil];

}

-(void)createSearchButton{
    _searchView = [[UIView alloc]initWithFrame:CGRectMake(0,_headView.bottom, KScreenW, 40)];
    [self.view addSubview:_searchView];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, _searchView.width-20, 35)];
//    + (UIColor *)darkGrayColor;   // 0.333 white
//    + (UIColor *)lightGrayColor;  // 0.667 white
//    + (UIColor *)whiteColor;      // 1.0 white
//    + (UIColor *)grayColor;       // 0.5 white
    button.backgroundColor = [UIColor lightGrayColor];
//    button.titleLabel.text = @"";
    [button setTitle:@"🔍搜索产品名称/目的地" forState:UIControlStateNormal];
    button.alpha = 0.3;
    button.titleLabel.textColor = [UIColor grayColor];
    [_searchView addSubview:button];

}

//---------------tableView------------------
-(void)createTableView{

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _searchView.bottom, KScreenW, self.view.frame.size.height-_headView.height-_searchView.height) style:UITableViewStyleGrouped];

    _tableView.delegate = self;
    _tableView.dataSource  = self;
    


#pragma -mark注册单元格  由于不是需要注册复用，故不注册复用
    //通过注册，产生（特定类的）单元格
//    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"PAndH_cell"];
    [self.view addSubview:_tableView];


}
#pragma mark- tableView delegate
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;
}
//单元格样子
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //分了8组，每组一个单元格
    //单元格就8个，而且都不一样，所以都每次alloc init
    if (indexPath.section == 0) {

        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PANDH_cell"]    ;
        cell.width = KScreenW;
        //滑动广告视图
        UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, cell.bounds.size.width,170)];
        scrollView.backgroundColor = [UIColor orangeColor];
        [cell.contentView addSubview:scrollView];

        SegmentView *segmentView = [[SegmentView alloc]initWithFrame:CGRectMake(10, scrollView.bottom+5,cell.bounds.size.width-20, 40) withTitles:@[@"热门",@"东南亚",@"欧美澳",@"国内"] segmentBlock:^(int index) {

            NSLog(@"%d",index);
        }];
        [cell.contentView addSubview:segmentView];

        //分割线
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(segmentView.frame.origin.x, segmentView.bottom, segmentView.width, 1)];
        label.backgroundColor = [UIColor grayColor];
        label.alpha = 0.3;
        [cell.contentView addSubview:label];

        //热门 东南亚 欧美澳 的下面的滑动式图
        UIScrollView *hotScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, label.bottom+5, label.width, 80)];
        hotScrollView.delegate = self;
        hotScrollView.contentSize = CGSizeMake(4*hotScrollView.width, hotScrollView.height);
//        hotScrollView.showsHorizontalScrollIndicator = NO;
        hotScrollView.clipsToBounds = NO;
        hotScrollView.pagingEnabled = YES;
        hotScrollView.bounces = NO;// 取消反弹效果
        [cell.contentView addSubview:hotScrollView];

        //热门 东南亚 欧美澳的滑动式图上的摁钮
        for (int i = 0; i<=(4*8-1); i++) {
//            UIButton *button = [UIButton alloc]initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)

        }


        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else{

        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PANDH_cell"];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

    }
}
//热门 东南亚 欧美澳的滑动式图上的摁钮 获得frame的方法

-(CGRect)getFrameWithIndex:(NSInteger)index{
    /*

     0-15:x = index*宽 +(index+1)*间隔
          y = 0
    16-31:x = index*（间隔+宽）
          y = index/16

     */
//    CGFloat xSpace = 5;
//    CGFloat ySpace = 10;

    /*
     x 0 , 7, 14, 21
     1   8  15  22
     y 0 -- 6
     7 -- 13
     */
    CGFloat xSpace = (KScreenW-20 - kButtonW *7)/8;
    CGFloat ySpace = (80 - kButtonH *2)/2;
    CGFloat x = xSpace * (index%7 +1) +kButtonW *(index%7);
    CGFloat y = ySpace * (index/7 +1) +kButtonH * (index/7);

    return CGRectMake(x, y, kButtonW, kButtonH);


}

//tableView分组 组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 8;
}

//cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 330;
}

//设置组的头试图的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;//这样能保证组的头视图为20的小高度
}
//cell被点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"+++++点击了 %@ 的cell",indexPath);
}

//滑动改变假的搜索框的位置
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat yOffset = scrollView.contentOffset.y;
    NSLog(@"y = %f",yOffset);

#warning 实在是难搞，找伟哥商量一下

    if (yOffset == 0) {

        _searchView.transform = CGAffineTransformIdentity;
        _searchView.frame = CGRectMake(0,_headView.bottom, KScreenW, 40);
    }
}

//----------第一个单元格的样子-------------
//-(UIView*)firstCell{
//
////    UIView *view = [UIView alloc]initWithFrame:<#(CGRect)#>
//
//}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
